﻿using Stock;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Vouchers.OutStockVouchers;
using Vouchers.AllotVouchers;
using Stock.Input;
using Stock.Output;
using Volo.Abp.Domain.Repositories;
using Microsoft.EntityFrameworkCore;

namespace Vouchers
{
    public class AllotVoucherService : ApplicationService, IAllotVoucherService, IAllotVoucherStockService
    {
        private readonly IRepository<AllotVoucher, Guid> _allotRepository;
        private readonly IRepository<AllotVoucherDetail, Guid> _allotDetailRepository;
        public AllotVoucherService(IRepository<AllotVoucher, Guid> allotRepository, IRepository<AllotVoucherDetail, Guid> allotDetailRepository)
        {
            _allotRepository = allotRepository;
            _allotDetailRepository = allotDetailRepository;
        }
        public async Task<List<AllotStockOutput>> GetAllotStock(GetStockInput input)
        {
            var query = from a in (await _allotRepository.GetQueryableAsync())
                        join b in (await _allotDetailRepository.GetQueryableAsync()) on a.VoucherCode equals b.VoucherCode
                        where input.VoucherCodes.Contains(a.VoucherCode)
                        select new AllotStockOutput
                        {
                            InDeptId = a.InDeptId,
                            InDeptName = a.InDeptName,
                            OutDeptId = a.OutDeptId,
                            OutDeptName = a.OutDeptName,
                            ProductCode = b.ProductCode,
                            ProductName = b.ProductName,
                            AllotStockCount = b.AllotCount,
                            Price = b.Price,
                            ProductBatch = b.ProductBatch,
                            ProductDate = b.ProductDate,
                            ExpiryDate = b.ExpiryDate,
                            TrackCode = b.TrackCode,
                            SupplierId = b.SupplierId ,
                            SupplierName = b.SupplierName,
                            ProductClassName = a.ProductClassName,
                            ProductClassId = a.ProductClassId,
                            ManufactureId = b.ManufacturerId,
                            ManufactureName = b.ManufacturerName,
                            Specification = b.Specification,
                            UseUnit = b.Unit
                        };

            var dto = await query.ToListAsync();
            return dto;
        }
    }
}
